package cn.school.controller.iot;

import cn.school.common.QueryBaseVo;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.iot.param.StudentRulePageParam;
import cn.school.iot.service.FaceGateRuleDeviceService;
import cn.school.iot.service.FaceGateRuleTimePeriodService;
import cn.school.iot.service.IotDeviceOperateLogService;
import cn.school.iot.vo.FaceGateRuleTimePeriodReceiveVo;
import cn.school.iot.vo.FaceGateRuleTimePeriodVo;
import cn.school.iot.vo.StudentRuleVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 门禁规则Controller
 * @ClassName: FaceGateRuleController
 * @Author: tzw
 * @Date: 2024/4/1 14:00
 */
@RestController
@RequestMapping("/faceGateRule")
public class FaceGateRuleController {
    @Resource
    private FaceGateRuleTimePeriodService faceGateRuleTimePeriodService;
    @Resource
    private FaceGateRuleDeviceService faceGateRuleDeviceService;
    @Resource
    private IotDeviceOperateLogService iotDeviceOperateLogService;

    @RequestMapping("findList")
    @GetMapping
    public CommonResult<CommonPage<FaceGateRuleTimePeriodVo>> findList(FaceGateRuleTimePeriodVo vo, QueryBaseVo queryVo) {
        IPage<FaceGateRuleTimePeriodVo> page = this.faceGateRuleTimePeriodService.getList(vo, queryVo);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @RequestMapping("info/{id}")
    @GetMapping
    public CommonResult<FaceGateRuleTimePeriodVo> getInfo(@PathVariable Long id) {
        FaceGateRuleTimePeriodVo vo = this.faceGateRuleTimePeriodService.getById(id);
        if (vo == null) {
            return CommonResult.failed("未查询到数据");
        }
        return CommonResult.success(vo);
    }

    @ApiOperation("addOrUpdateDevicRule => 新增或修改设备时间规则信息")
    @PostMapping(value = "/addOrUpdateDevicRule")
    public CommonResult<CommonPage> addOrUpdateDevice(@RequestBody FaceGateRuleTimePeriodReceiveVo vo) {
        int num = this.faceGateRuleTimePeriodService.addOrUpdateDeviceRule(vo);
        if(num>0){
            return CommonResult.success();
        }else if (num == -1){
            return CommonResult.failed("规则名称重复，请更改规则名称后再试");
        }
        return CommonResult.failed("提交失败，请联系管理员");
    }

    @ApiOperation("deleteFaceRule => 删除设备规则信息")
    @DeleteMapping(value = "/deleteFaceRule/{id}")
    public CommonResult<CommonPage<FaceGateRuleTimePeriodVo>> deleteDevice(@PathVariable Long id) {
        int num = this.faceGateRuleTimePeriodService.deleteById(id);
        if(num>0){
            return CommonResult.success();
        }else if(num == 0){
            return CommonResult.failed("当前规则正在被使用，请处理后重试");
        }
        return CommonResult.failed("提交失败，请联系管理员");
    }

    @PostMapping("/addGateRuleDevice")
    public CommonResult<CommonPage> addGateRuleDevice(@RequestBody String records) {
        List map = JSON.parseObject(records, List.class);
        boolean result = this.faceGateRuleDeviceService.addOrUpdateDeviceRule(map);
        if(result){
            return CommonResult.success();
        }
        return CommonResult.failed("提交失败，请联系管理员");
    }

    @PostMapping("/findStuList")
    public CommonResult<CommonPage<StudentRuleVo>> getStudentInfo(@RequestBody StudentRulePageParam param){
        return faceGateRuleTimePeriodService.getStudentInfo(param);
    }

    @GetMapping("/syncRule/{type}")
    public CommonResult<CommonPage> syncRule(@PathVariable String type){
        return faceGateRuleTimePeriodService.syncFaceGateRule(type);
    }

    @ApiOperation(value = "receiveStudentRuleRecords => 接收门禁规则")
    @PostMapping("/receiveGateRule")
    public CommonResult<CommonPage> receiveGateRule(@RequestBody String records){
        return faceGateRuleTimePeriodService.receiveGateRule(records);
    }

    @ApiOperation(value = "receiveStudentRuleRecords => 接收学生绑定规则")
    @PostMapping(value = "/receiveStudentRuleRecords")
    public CommonResult<CommonPage> receiveStudentRuleRecords(@RequestBody String records) {
        return faceGateRuleTimePeriodService.receiveStudentRuleRecords(records);
    }

}
